﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Linq.Expressions;
using System.IO;

namespace RAMED
{
    public partial class RecherchelotForm : Form
    {
        modifierlotform mlf = new modifierlotform();
    


        public List<Lot> RechercheLot = new List<Lot>();
         public RecherchelotForm()
        {
           
            InitializeComponent();
            RAMEDEntities1 context = SingletonContext.GetContext();
            comboBoxannexelot.DataSource = context.Annexe_administratif;
            comboBoxannexelot.DisplayMember = "nom_annexe_administratif";
            comboBoxannexelot.SelectedItem = null;
             
        }

        private void btnrecherche_Click(object sender, EventArgs e)
        {
            RAMEDEntities1 context = SingletonContext.GetContext();
            List<Lot> leslots = new List<Lot>();
            leslots = context.Lots.ToList<Lot>();

            if (comboBoxannexelot.SelectedItem != null)
            {
                Annexe_administratif annexeadministrative = new Annexe_administratif();
                annexeadministrative = (Annexe_administratif)comboBoxannexelot.SelectedValue;
                leslots = leslots.Where(p => p.id_lot == annexeadministrative.id_annexe_administratif).ToList<Lot>();

            }

           if (checkBoxdateinf.Checked)
            {
                
                leslots = leslots.Where(c => c.date_traitement.Value.Date >= dateTimePickerdatelotinf.Value.Date).ToList();
           }
           if (checkBoxdatesup.Checked)
            {
               leslots = leslots.Where(c => c.date_traitement.Value <= dateTimePickerdatelotsup.Value).ToList();
            }

            if (!txtnum.Text.Trim().Equals(""))
            {

                Lot lot = new Lot();
                int numlotsaisi = utitlitaire.CasterString(txtnum.Text);
                leslots = leslots.Where(p => p.num_de_lot == numlotsaisi).ToList<Lot>();

            }

         RechercheLot = leslots;
         mettreAjouterFormGridViewlot();
        }

        private void remplirDataGridView()
        {
            dataGridViewrechlot.AutoGenerateColumns = false;
            dataGridViewrechlot.ColumnCount = 3;
            dataGridViewrechlot.Columns[0].HeaderText = "numero de lot";
            dataGridViewrechlot.Columns[0].DataPropertyName = "num_de_lot";

            dataGridViewrechlot.Columns[1].HeaderText = "Total de lot";
            dataGridViewrechlot.Columns[1].DataPropertyName = "total_do_lot";

            dataGridViewrechlot.Columns[2].HeaderText = "date de traitement";
            dataGridViewrechlot.Columns[2].DataPropertyName = "date_traitement";


            dataGridViewrechlot.DataSource = RechercheLot;
        }

        private void RecherchelotForm_Load(object sender, EventArgs e)
        {
          
        }
        public void mettreAjouterFormGridViewlot()
        {
            dataGridViewrechlot.DataSource = null;
            remplirDataGridView();
            dataGridViewrechlot.Update();
            dataGridViewrechlot.Refresh();
        }

        private void comboBoxannexelot_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void dateTimePickerdatelot_ValueChanged(object sender, EventArgs e)
        {
            

        }

        private void checkBoxannexelot_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBoxdatelotinf_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnbodifier_Click(object sender, EventArgs e)
        {
            if (dataGridViewrechlot.CurrentRow == null)
            {
                NotificationTest.NotificationManager nm = new NotificationTest.NotificationManager();
                nm.Show(this, "Veuillez sélectionner un enregistrement pour le modifier", Color.Red, 3000);
            }
            else
            {

                RAMEDEntities1 context = SingletonContext.GetContext();
            Lot lo = new Lot();

            int id_lot = ((Lot)dataGridViewrechlot.CurrentRow.DataBoundItem).id_lot;
            lo = context.Lots.Where(d => d.id_lot == id_lot).First();

            mlf = new modifierlotform();
            mlf.Lo = lo;
            mlf.parent = this;
            mlf.InitialiserChamps();
            mlf.Show();
        }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void Button21_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void dateTimePickerdatelotsup_ValueChanged(object sender, EventArgs e)
        {
            
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void txtnum_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridViewrechlot_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }

}
